using System;
using System.Collections.Generic;
using System.Text;

namespace GameEditor.Common
{
    public struct Parameter
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private object value;

        public object Value
        {
            get { return value; }
            set { this.value = value; }
        }
        public override string ToString()
        {
            return string.Format("[{0}]", name);
        }
        public Parameter(string name, object value)
        {
            this.name = name;
            this.value = value;
        }

    }
    public class Parameters : List<Parameter>
    {
        public Parameters()
        {
        }
        public Parameters(params Parameter[] parameters)
        {
            AddRange(parameters);
        }
        public object this[string name]
        {
            get
            {
                Nullable<Parameter> n = Lookup(name);
                if (n == null)
                    return null;
                return n.Value.Value;
            }
        }
        public void Add(string name,object value)
        {
            int i = IndexOf(name);
            if (i == -1)
            {
                base.Add(new Parameter(name, value));
            }
            else
            {
                this[i] = new Parameter(name, value);
            }
        }
        public new void Add(Parameter parameter)
        {
            int i = IndexOf(parameter.Name);
            if (i == -1)
            {
                base.Add(parameter);
            }
            else
            {
                this[i] = parameter;
            }
        }
        public int IndexOf(string name)
        {
            for (int i = 0; i < Count; i++)
            {
                if (string.Compare(this[i].Name, name) == 0)
                    return i;
            }
            return -1;
        }
        public Nullable<Parameter> Lookup(string name)
        {
            int index = IndexOf(name);
            if (index == -1)
                return null;
            return this[index];
        }

        public static implicit operator Parameter[](Parameters parameters)
        {
            return parameters.ToArray();
        }
    }
}
